#include "stdafx.h"
#include "Frame.h"
#include "resource.h"

static const int BorderWH=2;

CFrame::CFrame()
{
	hWnd=NULL;

	WNDCLASSEX window;

	window.cbSize = sizeof (window);
	window.style = CS_HREDRAW | CS_VREDRAW;
	window.lpfnWndProc = (WNDPROC)Procedure;
	//window.lpfnWndProc=NULL;
	window.cbClsExtra = 0;
	window.cbWndExtra = 0;
	window.hInstance = GetAppInstance();
	window.hIcon = NULL /*LoadIcon (hInstance, MAKEINTRESOURCE (IDI_ICON1))*/;
	window.hCursor = LoadCursor(NULL, IDC_ARROW);
	window.hbrBackground = NULL;
	window.lpszMenuName = NULL;
	window.lpszClassName = _T("CFrame");
	window.hIconSm = NULL/*(HICON) LoadImage (hInstance, MAKEINTRESOURCE (IDI_ICON1), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR)*/;

	RegisterClassEx (&window);
}

CFrame::~CFrame(void)
{
}

HWND CFrame::Create(int x, int y, int nWidth, int nHeight, HWND hWndParent)
{
	hParent=hWndParent;
	hWnd=CreateWindow(_T("CFrame"),_T("frame"), WS_CHILD | WS_VISIBLE /*| WS_BORDER*/, x, y, nWidth, nHeight, hWndParent, NULL, GetAppInstance(), this);

	return hWnd;
}

LRESULT CALLBACK CFrame::Procedure( HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam )
{
	CREATESTRUCT* pCS;
	CFrame* pWindow = NULL;

	switch(iMsg)
	{
	case WM_NCCREATE:
		pCS = (CREATESTRUCT*) lParam;
		pWindow = (CFrame*) pCS->lpCreateParams;

		if(pWindow)
		{
			// Save it in the window handle.
			pWindow->SetWindowPtr(hwnd, pWindow);
			// Store the window handle in the object.
			pWindow->hWnd = hwnd;
		}
		break;
	default:
		// Extract the object from the window handle.
		pWindow = (CFrame*) GetWindowPtr(hwnd);
	}

	// Let the object handle it.
	if(pWindow)
		return pWindow->OnMessage(iMsg,wParam,lParam);
	// Just do the default if the window is not created yet.
	else return DefWindowProc(hwnd, iMsg, wParam, lParam);

	//return FALSE;
}

LRESULT CFrame::OnMessage(UINT iMsg, WPARAM wParam, LPARAM lParam)
{
	switch(iMsg)
	{
	case WM_COMMAND:
		OnCommand(LOWORD(wParam),HIWORD(wParam));
		return FALSE;
	//case WM_NOTIFY:
	//	return FALSE;
	case WM_CREATE:
		OnCreate(GetWnd(),iMsg,wParam,lParam);
		return FALSE;
	case WM_PAINT:
		OnPaint();
		return FALSE;
	case WM_ERASEBKGND:
		return FALSE;	// never redraw the background, avoid clipping errors
	case WM_NCCALCSIZE:
		if(wParam)
		{
			NCCALCSIZE_PARAMS *p=(NCCALCSIZE_PARAMS*)lParam;
			InflateRect((LPRECT)&p->rgrc[0], -BorderWH, -BorderWH);
			//InflateRect((LPRECT)&p->rgrc[1], -BorderWH, -BorderWH);
			//InflateRect((LPRECT)&p->rgrc[2], -BorderWH, -BorderWH);
			//return DefWindowProc(hWnd,iMsg,wParam,(LPARAM)p);
		}
		break;
	case WM_NCPAINT:
		{
			HDC hdc=GetWindowDC(hWnd);
			OnNCPaint(hdc);
			ReleaseDC(hWnd, hdc);
		}
		return FALSE;
	//case WM_NCACTIVATE:
	//	return FALSE;	// enable us to draw the non client area
	case WM_KILLFOCUS:
		OnKillFocus();
		return FALSE;
	case WM_SETFOCUS:
		OnSetFocus();
		return FALSE;
	case WM_VSCROLL:
		OnVScroll(LOWORD(wParam),HIWORD(wParam));
		return FALSE;
	case WM_HSCROLL:
		OnHScroll(LOWORD(wParam),HIWORD(wParam));
		return FALSE;
	case WM_LBUTTONDOWN:
		SetFocus(*this);
		OnLButtonDown(LOWORD( lParam ), HIWORD( lParam ));
		return FALSE;
	case WM_LBUTTONUP:
		OnLButtonUp(LOWORD( lParam ), HIWORD( lParam ));
		return FALSE;
	case WM_MBUTTONDOWN:
		SetFocus(*this);
		OnMButtonDown(LOWORD( lParam ), HIWORD( lParam ));
		return FALSE;
	case WM_MBUTTONUP:
		OnMButtonUp(LOWORD( lParam ), HIWORD( lParam ));
		return FALSE;
	case WM_RBUTTONDOWN:
		SetFocus(*this);
		OnRButtonDown(LOWORD( lParam ), HIWORD( lParam ));
		return FALSE;
	case WM_RBUTTONUP:
		OnRButtonUp(LOWORD( lParam ), HIWORD( lParam ));
		return FALSE;
	case WM_MOUSEMOVE:
		OnMouseMove((int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam));
		return FALSE;
	case WM_MOUSEWHEEL:
		OnMouseWheel(GET_KEYSTATE_WPARAM(wParam), GET_WHEEL_DELTA_WPARAM(wParam), (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam));
		return FALSE;
	//case WM_KEYDOWN:
 //		OnKeyDown(wParam,lParam);
	//	return FALSE;
	case WM_SIZE:
		if(wParam != SIZE_MINIMIZED)
			OnSize(LOWORD(lParam),HIWORD(lParam));
		return FALSE;
	case WM_DRAWITEM:
		OnDrawItem((LPDRAWITEMSTRUCT) lParam);
		return TRUE;
	case WM_MEASUREITEM:
		OnMeasureItem((int)wParam,(LPMEASUREITEMSTRUCT) lParam); 
		return TRUE;
	case WM_INITMENUPOPUP:
		OnInitMenuPopUp((HMENU)wParam,lParam);
		return FALSE;
	//case WM_GETDLGCODE:
		//MessageBox(_T("Cazzi"));
	}

	//return FALSE;
	return DefWindowProc(hWnd, iMsg, wParam, lParam);
}

void CFrame::OnPaint()
{
	PAINTSTRUCT ps;
	HDC hdc = BeginPaint (hWnd, &ps);

	Draw(hdc);

	EndPaint(hWnd,&ps);
}

void CFrame::OnNCPaint(HDC hdc)
{
	RECT rect;
	GetClientRect(hWnd,&rect);
	rect.right+=BorderWH*2;
	rect.bottom+=BorderWH*2;
	CBrush b, c;
	b.CreateSolidBrush(RGB(167,166,170));
	c.CreateSolidBrush(RGB(235,233,237));
	//DrawEdge(hdc, &rect,EDGE_RAISED /*EDGE_SUNKEN*/, BF_RECT);
	//FrameRect(hdc,&rect,b);
	//InflateRect(&rect,-1,-1);
	FrameRect(hdc,&rect,c);
	InflateRect(&rect,-1,-1);
	FrameRect(hdc,&rect,b);
}

void CFrame::Resize(int x, int y, int w, int h)
{
	MoveWindow(hWnd,x,y,w,h,true);

	ClientSize.cx=w;
	ClientSize.cy=h;

	//if(m_Scroll_h)
	//{
	//	int cyh = GetSystemMetrics (SM_CYHSCROLL);
	//	int cxv = GetSystemMetrics (SM_CXVSCROLL);
	//	//MoveWindow(m_Scroll_h,x,ClientSize.cy-cyh+y+BorderWH,ClientSize.cx-cxv-BorderWH,cyh,FALSE);
	//	//RedrawWindow (m_Scroll_h, NULL, NULL, RDW_INVALIDATE | RDW_ERASENOW | RDW_ALLCHILDREN);
	//}
	//if(m_Scroll_v)
	//{
	//	int cyh = GetSystemMetrics (SM_CYHSCROLL);
	//	int cxv = GetSystemMetrics (SM_CXVSCROLL);
	//	MoveWindow(m_Scroll_v,ClientSize.cx-cxv+x-BorderWH,y+BorderWH,cxv,ClientSize.cy-cyh-BorderWH,FALSE);
	//	RedrawWindow (m_Scroll_v, NULL, NULL, RDW_INVALIDATE | RDW_ERASENOW | RDW_ALLCHILDREN);
	//}

	//InvalidateRect(GetWnd(), NULL, FALSE );
	//UpdateWindow(GetWnd());
}
